using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MicroMsg.UI.Util
{
	public class ListBoxHelper
	{
		public static DependencyObject FindScrollerView(DependencyObject dependencyObject_0)
		{
			DependencyObject dependencyObject = null;
			int childrenCount = VisualTreeHelper.GetChildrenCount(dependencyObject_0);
			int i = 0;
			while (i < childrenCount)
			{
				DependencyObject child = VisualTreeHelper.GetChild(dependencyObject_0, i);
				if (!(child is ScrollViewer))
				{
					dependencyObject = ListBoxHelper.FindScrollerView(child);
					if (dependencyObject == null)
					{
						i++;
						continue;
					}
				}
				else
				{
					dependencyObject = child;
				}
				return dependencyObject;
			}
			return dependencyObject;
		}
		public static int FindFirstVisibleIndex(ListBox listbox)
		{
			int num = -1;
			FrameworkElement frameworkElement = (FrameworkElement)listbox.get_ItemContainerGenerator().ContainerFromIndex(0);
			ScrollViewer scrollViewer = (ScrollViewer)ListBoxHelper.FindScrollerView(listbox);
			if (frameworkElement != null && scrollViewer != null)
			{
				num = (int)Math.Ceiling(scrollViewer.get_VerticalOffset()) - 1;
			}
			if (num < 0 || num >= listbox.get_Items().get_Count())
			{
				num = -1;
			}
			return num;
		}
	}
}
